﻿using Microsoft.UI.Xaml.Automation.Peers;

namespace DimensionsHelper.Client.WinUi.Helpers;

public static class UIHelper
{
    public static T? FindParent<T>(UIElement element) where T : UIElement
    {
        DependencyObject? parent = VisualTreeHelper.GetParent(element);

        while (parent != null)
        {
            if (parent is T target)
            {
                return target;
            }

            parent = VisualTreeHelper.GetParent(parent);
        }

        return null;
    }


    public static void AnnounceActionForAccessibility(UIElement element, string announcement, string activityId)
    {
        AutomationPeer? peer = FrameworkElementAutomationPeer.FromElement(element);
        peer.RaiseNotificationEvent(AutomationNotificationKind.ActionCompleted,
            AutomationNotificationProcessing.ImportantMostRecent, announcement, activityId);
    }
}